package com.easpeed.comon.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.easpeed.comon.memorycache.CommonMemoryCache
import com.easpeed.elevator.R
import com.easpeed.elevator.utils.NoticeDialog
import com.easpeed.comon.photo.PhotoUtils
import com.easpeed.elevator.bean.RouteBean
import com.easpeed.elevator.databinding.DialogRouteListBinding
import com.easpeed.elevator.utils.WwAdapter
import com.easpeed.elevator.utils.WwViewHolder
import com.easpeed.user.utils.FileHelper


/**
 * Created by bigyu on 2024/11/12 13:37
 * com.easpeed.comon.dialog
 */
class RouteListDialog {
    var dialogEventTable: NoticeDialog? = null

    private var mContext: Context? = null

    private var photoUtils: PhotoUtils? = null

//    private var cacheRouteList: ArrayList<RouteBean> = arrayListOf()

    var binding: DialogRouteListBinding? = null
    fun show(mContext: Context, routeList: List<RouteBean>) {

        val tempList: ArrayList<RouteBean> = arrayListOf()

        routeList.forEach { data ->
            val findBean = tempList.find { it.distance == data.distance }
            if (findBean == null) {
                tempList.add(data)
            }
        }

        photoUtils = PhotoUtils(mContext)
//        cacheRouteList.clear()
//        cacheRouteList.addAll(FileHelper.readRouteData(mContext))

        this.mContext = mContext
        if (dialogEventTable == null) {
            dialogEventTable = NoticeDialog.Builder(mContext).setLayoutId(R.layout.dialog_route_list).setClickBtnDismissDialog(true).setCanCancel(true).setCanceledOnTouchOutside(true).create()
            dialogEventTable?.setOnclickListener(R.id.close, View.OnClickListener { v: View? ->
                dialogEventTable?.dismiss()
            })
            dialogEventTable?.setDialogWith((ScreenUtils.getScreenWidth() * 0.8f).toInt(), (ScreenUtils.getScreenHeight() * 0.8f).toInt())
            dialogEventTable?.setGravity(Gravity.CENTER)
            dialogEventTable?.setCanceledTouchOutside(false)
        }

        if (dialogEventTable?.isShowing != true) {
            dialogEventTable?.show()
        }

        binding = dialogEventTable?.getView(R.id.root)?.let { DataBindingUtil.bind(it) }


//        val routeList = arrayListOf(RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean(), RouteBean())
        initOpticalView(binding?.recylerview!!, tempList)
    }

    private fun initOpticalView(recyclerView: RecyclerView, opticalList: List<RouteBean>?, callback: ((List<RouteBean>) -> Unit)? = null) {
        var tempList: List<RouteBean>? = opticalList
        if (tempList == null) {
            tempList = arrayListOf()
        }
        var wwAdapterOptical: WwAdapter<RouteBean>? = null
        wwAdapterOptical = object : WwAdapter<RouteBean>() {

            override fun initRecyclerView(mContext: Context, adapter: WwAdapter<RouteBean>) {
                val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
                recyclerView.layoutManager = manager
                recyclerView.adapter = adapter
            }

            override fun getLayoutId(): Int {
                return R.layout.dialog_route_list_info
            }

            @SuppressLint("NotifyDataSetChanged")
            override fun convert(holder: WwViewHolder, bean: RouteBean, position: Int) {
                holder.setText(R.id.coordinate, "${bean.lat},${bean.lng}")
                holder.setText(R.id.number, (position + 1).toString())
                holder.setText(R.id.strength, bean.strength)
                holder.setText(R.id.location, bean.distance)
                holder.setOnClickListener(R.id.location_icon, View.OnClickListener { view: View? ->
                    bean.lat = "${CommonMemoryCache.devicelat}"
                    bean.lng = "${CommonMemoryCache.devicelon}"
                    wwAdapterOptical?.notifyDataSetChanged()
                })
                holder.setOnClickListener(R.id.coordinate_icon, View.OnClickListener { view: View? ->
                    bean.lat = "${CommonMemoryCache.devicelat}"
                    bean.lng = "${CommonMemoryCache.devicelon}"
                    wwAdapterOptical?.notifyDataSetChanged()
                    val routeUploadDialog = RouteUploadDialog()
                    mContext?.let {
                        routeUploadDialog.show(it, bean, false) { routeBean, type ->
//                            cacheRouteList.remove(bean)
//                            cacheRouteList.add(bean)
//                            FileHelper.saveRouteData(mContext, cacheRouteList)
//                            ToastUtils.showShort("上传成功")
                        }
                    }
                })
            }
        }.create(mContext, tempList)
    }
}